//
//  NewViewController.m
//  SecretAlbums
//
//  Created by BabyFinancial on 15/6/11.
//  Copyright (c) 2015年 BabyFinancial. All rights reserved.
//

#import "NewViewController.h"
#import <POP/POP.h>
#import "SecretAlbumsLogic.h"

@interface NewViewController ()

@property (nonatomic, strong) DotsView *againDotsView;
@property (nonatomic, assign) BOOL interactionEnabled;
@property (nonatomic, assign) BOOL isAgainEnter;

@end

@implementation NewViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.interactionEnabled = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    self.view.tintColor = SAColor(254.0f, 128.0f, 55.0f);
    self.circleBtnTitleColor = [UIColor blackColor];
    
    // 设置标题文本
    self.titleLable.text = NSLocalizedString(@"EnterNewPassword", nil);
    self.titleLable.textColor = [UIColor blackColor];
    
    // 设置Cancel按钮
    [self.actionBtn setTitle:NSLocalizedString(@"Cancel", nil) forState:UIControlStateNormal];
    [self.actionBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.actionBtn addTarget:self action:@selector(cancelBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    // 添加againDotsView
    self.againDotsView = [[DotsView alloc] initWithAmount:6];
    [self.againDotsView setTranslatesAutoresizingMaskIntoConstraints:NO];
    self.againDotsView.delegate = self;
    [self.view addSubview:self.againDotsView];
    
    [self.againDotsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view).centerOffset(CGPointMake(-self.view.frame.size.width, Iphone4OrOtherAdapter_Y(-160, -170)));
    }];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
}

#pragma mark - 私有方法

- (void)cancelBtnOnclick:(UIButton *)button
{
    if (self.delegate && [self.delegate respondsToSelector:@selector(cancelCreatePassword:)]) {
        [self.delegate cancelCreatePassword:self];
    }
}

- (void)movetoAgainEnter
{
    self.interactionEnabled = NO;
    
    POPSpringAnimation *firstAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    firstAnimation.toValue = @(self.view.frame.size.width * 1.5f);
    firstAnimation.springBounciness = 10;
    [self.dotsView.layer pop_addAnimation:firstAnimation forKey:@"firstAnimation"];
    
    POPSpringAnimation *againAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    againAnimation.toValue = @(self.view.frame.size.width * 0.5f);
    againAnimation.springBounciness = 10;
    [againAnimation setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
        self.isAgainEnter = YES;
        self.interactionEnabled = YES;
    }];
    [self.againDotsView.layer pop_addAnimation:againAnimation forKey:@"againAnimation"];
}

- (void)movetoFirstEnter
{
    self.interactionEnabled = NO;
    
    POPSpringAnimation *firstAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    firstAnimation.toValue = @(self.view.frame.size.width * 0.5f);
    firstAnimation.springBounciness = 10;
    [firstAnimation setCompletionBlock:^(POPAnimation *animation, BOOL finished) {
        self.isAgainEnter = NO;
        self.interactionEnabled = YES;
    }];
    [self.dotsView.layer pop_addAnimation:firstAnimation forKey:@"firstAnimation"];
    
    POPSpringAnimation *againAnimation = [POPSpringAnimation animationWithPropertyNamed:kPOPLayerPositionX];
    againAnimation.toValue = @(-self.view.frame.size.width * 1.5f);
    againAnimation.springBounciness = 10;
    [self.againDotsView.layer pop_addAnimation:againAnimation forKey:@"againAnimation"];
}

#pragma mark - CircleButtonDelegate

- (void)touchCircleButton:(CircleButton *)circleButton
{
    if (!self.interactionEnabled) return;
    
    if (self.isAgainEnter) {
        [self.againDotsView operateFromCircleButton:circleButton];
    } else {
        [self.dotsView operateFromCircleButton:circleButton];
    }
}

#pragma mark - DotsViewDelegate

- (BOOL)verifyPassword:(NSString *)password FromDotsView:(DotsView *)dotsView
{
    return YES;
}

- (void)successPassword:(NSString *)password FromDotsView:(DotsView *)dotsView
{
    if (dotsView == self.dotsView) {
        [self movetoAgainEnter];
        
        self.titleLable.text = NSLocalizedString(@"EnterNewPasswordAgain", nil);
    } else if (dotsView == self.againDotsView) {
        if ([self.againDotsView.password isEqualToString:self.dotsView.password]) {
            [SecretAlbumsLogic createAlbumWithID:self.againDotsView.password];
            if (self.delegate && [self.delegate respondsToSelector:@selector(successPassword:newViewController:)]) {
                [self.delegate successPassword:self.againDotsView.password newViewController:self];
            }
        } else {
            [self movetoFirstEnter];
            
            [self.dotsView cleanPassWord];
            [self.againDotsView cleanPassWord];
            
            self.titleLable.text = NSLocalizedString(@"PasswordMismatch", nil);
        }
    }
}

@end
